package com.xiaofu.component.impl;

import com.xiaofu.component.SmsService;
import com.xiaofu.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author luofuzhou
 * @date 2024/7/4
 * @dec 描述
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Override
    public void sendMsg(String to,String code) {
        String host = "https://jmsms.market.alicloudapi.com";
        String path = "/sms/send";
        String method = "POST";
        String appcode = "0d4599b29a8043c5b039c2eaab560db7";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", to);
        querys.put("templateId", "JM1000372");
        querys.put("value", code);
        Map<String, String> bodys = new HashMap<String, String>();
        try {
            HttpResponse response = HttpUtils.doPost(host, path,method,headers,querys,bodys);
            log.info(response.toString());
            //获取response的body
            log.info(EntityUtils.toString(response.getEntity()));
            log.info("短信发送成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
